package com.now.printer.base.BaseAdapter;

import android.content.Context;
import android.view.ViewGroup;


import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;

import butterknife.ButterKnife;

public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseHolder> {
    public Context context;
    List<T> data;
    int[] layoutIds;//布局资源Id数组layoutIds中
//    private OnItemClickLitener mOnItemClickLitener;

    public BaseAdapter(Context context, List<T> data, int layoutId) {
        this.context = context;
        this.data = data;
        //不使用多布局时的构造函数，将layoutId放到布局资源Id数组layoutIds中
        this.layoutIds = new int[]{layoutId};
    }

//    //事件回调
//    public interface OnItemClickLitener {
//        void onItemClick(View view, int position);
//    }

//    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
//        this.mOnItemClickLitener = mOnItemClickLitener;
//    }

    //使用多布局时的构造函数，可以接收传入的布局资源Id数组
    public BaseAdapter(Context context, List<T> data, int[] layoutIds) {
        this.context = context;
        this.data = data;
        this.layoutIds = layoutIds;
    }

    public void setData(List<T> data) {
        this.data.clear();
        this.data = data;
//        this.data.addAll(data);
        notifyDataSetChanged();
    }

    public void addData(List<T> data) {
//        this.data.clear();
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    public void removeData(int position) {
//        this.data.clear();
        this.data.remove(position);
        notifyDataSetChanged();
    }

    public void clearData() {
        this.data.clear();
        notifyDataSetChanged();
    }
    public List<T> getData() {
        return this.data;
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    @NotNull
    @Override
    public BaseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //根据viewType从布局资源数组中加载指定的布局
        //1.当不需要加载多布局时，子类继承BaseAdapter，不用覆写getItemViewType()
        //此时，viewType值默认为0；
        //2.当需要加载多布局时，子类继承MultiLayoutsBaseAdapter，需要覆写getItemViewType()
        //此时，viewType值为子类adapter指定的值；
        return BaseHolder.getHolder(context, parent, layoutIds[viewType]);
    }

    @Override
    public void onBindViewHolder(final BaseHolder holder, final int position) {
        ButterKnife.bind(this, holder.getLayout());
//        AutoUtils.autoSize(holder.getLayout());
        onBind(holder, data.get(position), position);
    }

    public abstract void onBind(BaseHolder holder, T t, int position);




    @Override
    public void onBindViewHolder(@NonNull BaseHolder holder, int position, @NonNull List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
    }
}
